/*
 * ExplosionObserver.cpp
 *
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses 
 */

#include "ExplosionObserver.h"
#include <sstream>
#include "Bomb.h"
#include "Message.h"
#include <iostream>

ExplosionObserver::ExplosionObserver(Partida& game_room, Map& map)
    : game_room(game_room), map(map) {

}

void ExplosionObserver::update(Event event, void* entity, void* other) {
    if (event == BOMB_EXPLODED) {
        Bomb* bomb = (Bomb*) entity;
        int x = bomb->getX();
        int y = bomb->getY();
        int range = bomb->getRange();
        /************************LOG************************/
        std::cout << "Explotó bomba " << bomb->getId() << " en (" << x << "," << y << ")\n";
        /***************************************************/
        Message message;
        message.code = DELETE;
        message.id = bomb->getId();
        game_room.notifyPlayers(message);
        MapEffect* explosion_effect = bomb->getExplosionEffect();
        map.applyEffect(explosion_effect, x, y - range);
        map.applyEffect(explosion_effect, x - range, y);
        map.applyEffect(explosion_effect, x, y);
        map.applyEffect(explosion_effect, x + range, y);
        map.applyEffect(explosion_effect, x, y + range);
        delete(explosion_effect);
    }
}
